
import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'
import store from '@/store'

const router = createRouter({
    history: createWebHistory(),
    routes,
    // 禁用返回
    scrollBehavior: () => {
        history.pushState(null, null, document.URL)
    }
})

router.beforeEach((to, from, next) => {
    const login = "/login";
    const notFount = "/404";
    const dashboard = "/";
    if (to.matched.length !== 0) {
        let token = store.getters.getToken;
        //系统路由,无需Token
        if (to.name == undefined || to.name == "") {
            if (to.path == login && token) {
                next(dashboard)
            }
            else {
                next()
            }
        }
        //需要登录
        else {
            if (token) {
                store.commit("setDefaultActive", to.path);
                store.commit("setBreadcrumb", to.matched.filter((n) => n.path !== dashboard));
                next()
            }
            else {
                next(login)
            }
        }
    } else {
        next(notFount)
    }
});

export default router;
